package com.vf.admin.utils.douyin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.vf.admin.utils.douyin.entity.DouYinShiPin;
import com.vf.admin.utils.douyin.entity.FanHui;

import java.util.HashMap;
import java.util.Map;

/*
 *抖音短连接获取详情无水印视频 
 */
public class XiangQingUtils {
    private String charset = "utf-8";  
    private HttpClientUtil httpClientUtil = null;  
      
    public XiangQingUtils(){  
        httpClientUtil = new HttpClientUtil();  
    }  
      
    public String test(String url){  
        String httpOrgCreateTest = url + "httpOrg/create";  
        Map<String,String> createMap = new HashMap<String,String>();
        	
//        createMap.put("Host","api.amemv.com"); 
//        createMap.put("Connection","keep-alive"); 
//        createMap.put("Cookie","odin_tt=f774a2e8a2e319031c621510fae1e9112f9b84a5cae34e6fc2ac0868cb7a91080e5542c252d7d687d6361662a067e1e7; sid_guard=ea6991be99cee16d3faf1ff28c5d55f8%7C1545138740%7C5184000%7CSat%2C+16-Feb-2019+13%3A12%3A20+GMT; uid_tt=1e88cc5560c85719458b3021825f9214; sid_tt=ea6991be99cee16d3faf1ff28c5d55f8; sessionid=ea6991be99cee16d3faf1ff28c5d55f8; install_id=56986543517; ttreq=1$cac0c9c22ef0f2c5061b5d0573bae3f6727ac059"); 
//        createMap.put("Accept-Encoding","gzip"); 
//        createMap.put("X-SS-REQ-TICKET","1546918804767"); 
//        createMap.put("X-Tt-Token","00ea6991be99cee16d3faf1ff28c5d55f8d6a5de97be39c0f182d2339875a6e155541697e3874598d5dbcba2d73991bb5a54"); 
//        createMap.put("sdk-version","1"); 
//        createMap.put("X-SS-RS","0"); 
//        createMap.put("User-Agent","com.ss.android.ugc.aweme/400 (Linux; U; Android 8.1.0; zh_CN; Redmi 6; Build/O11019; Cronet/58.0.2991.0)"); 
//        createMap.put("X-Khronos","1546920855"); 
//        createMap.put("X-Gorgon","01cc1b78f899167da7ae828e766dc15bdfe9c42c3aeba1469e"); 
//        createMap.put("X-Pods",""); 
        String httpOrgCreateTestRtn = httpClientUtil.doPost(httpOrgCreateTest,createMap,charset);  
        
//        System.out.println(httpOrgCreateTestRtn);  
        return httpOrgCreateTestRtn;
    }  
//      public static void main(String[] args){
    public String convert(String url){  
    	try{
    		//根据短连接获取一个长链接的参数
        	String urlfenxiang = url;//"http://v.douyin.com/YpkKu9/"; 
//    		String urlfenxiang = "http://v.douyin.com/YpkKu9/";
//        	System.out.println("分享的短url:"+urlfenxiang);
        	PageReader reader = new PageReader(); 
        	reader.connect(urlfenxiang); 
        	String content = reader.readContents();
        	reader.disconnect(); 
        	content = content.substring(content.indexOf("href=\"")+6, content.lastIndexOf("\">h"));
        	content = content.replaceAll("&amp;","&");
//        	System.out.println("短url重定向的分享url：" + content); 
        	String videoid = content.substring(content.indexOf("video/")+6, content.indexOf("/?region"));
//        	System.out.println("id:"+videoid);
        	
        	//拼接下一个链接
        	content = "https://api.amemv.com/aweme/v1/schema/trans/?url="+content;
        	content = content+"&ts=1546918804&js_sdk_version=1.6.4&app_type=normal&manifest_version_code=400&_rticket=1546918804782&ac=wifi&device_id=57400937868&iid=56986543517&mcc_mnc=46000&os_version=8.1.0&channel=update&version_code=400&device_type=Redmi6&language=zh&uuid=99001236660791&resolution=720*1344&openudid=22ea9722444e15ef&update_version_code=4002&app_name=aweme&version_name=4.0.0&os_api=27&device_brand=xiaomi&ssmix=a&device_platform=android&dpi=320&aid=1128&as=a1358163c4a92ccb346888&cp=1b9bc65c4e493fb7e1Si[m&mas=01a5e34b868b770206185d569c0d8a72791c1c1c6c2ccc46c6c64c";
//        	System.out.println("第二个url："+content);
        	
        	//拼接下一个url
        	String zhenshiurl = "https://api.amemv.com/aweme/v1/aweme/detail/?aweme_id="+videoid+"&retry_type=no_retry&mcc_mnc=46000&iid=56986543517&device_id=57400937868&ac=wifi&channel=update&aid=1128&app_name=aweme&version_code=400&version_name=4.0.0&device_platform=android&ssmix=a&device_type=Redmi+6&device_brand=xiaomi&language=zh&os_api=27&os_version=8.1.0&uuid=99001236660791&openudid=22ea9722444e15ef&manifest_version_code=400&resolution=720*1344&dpi=320&update_version_code=4002&_rticket=1546920855243&ts=1546920854&js_sdk_version=1.6.4&as=a1a592e33629bc43f42077&cp=2a99c35d614d323de1asOa&mas=01d91a7f526377af5559abf6f31b5a2295ecec0c4c2c66cc2cc646";
//        	System.out.println("拼接下一个url:"+zhenshiurl);
        	
        	XiangQingUtils main = new XiangQingUtils();  
            String result = main.test(zhenshiurl);  
            System.out.println(result);
            return result;
//            DouYinShiPin douyinShiPin = JSON.parseObject(result,DouYinShiPin.class);
//            System.out.println("douyinShiPin:"+douyinShiPin);
//            String miaoshu = douyinShiPin.getAweme_detail().getDesc();
//            String zheshirul = douyinShiPin.getAweme_detail().getVideo().getPlay_addr().getUrl_list().get(0).toString();
//            String zuozhe = douyinShiPin.getAweme_detail().getAuthor().getNickname();
//            String gequming = douyinShiPin.getAweme_detail().getMusic().getTitle();
//            FanHui  fanhui = new FanHui();
//            fanhui.setZuozhe(filterEmoji(zuozhe));
//            fanhui.setMiaoshu(filterEmoji(miaoshu));
//            fanhui.setDownloadurl(zheshirul);
//            fanhui.setGequming(filterEmoji(gequming));
//            return fanhui;
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return null;
    }  
    
    public FanHui convertWuShuiYin(String url){  
    	try{
    		//根据短连接获取一个长链接的参数
        	String urlfenxiang = url;//"http://v.douyin.com/YpkKu9/"; 
//    		String urlfenxiang = "http://v.douyin.com/YpkKu9/";
//        	System.out.println("分享的短url:"+urlfenxiang);
        	PageReader reader = new PageReader(); 
        	reader.connect(urlfenxiang); 
        	String content = reader.readContents();
        	reader.disconnect(); 
        	content = content.substring(content.indexOf("href=\"")+6, content.lastIndexOf("\">h"));
        	content = content.replaceAll("&amp;","&");
//        	System.out.println("短url重定向的分享url：" + content); 
        	String videoid = content.substring(content.indexOf("video/")+6, content.indexOf("/?region"));
//        	System.out.println("id:"+videoid);
        	
        	//拼接下一个链接
        	content = "https://api.amemv.com/aweme/v1/schema/trans/?url="+content;
        	content = content+"&ts=1546918804&js_sdk_version=1.6.4&app_type=normal&manifest_version_code=400&_rticket=1546918804782&ac=wifi&device_id=57400937868&iid=56986543517&mcc_mnc=46000&os_version=8.1.0&channel=update&version_code=400&device_type=Redmi6&language=zh&uuid=99001236660791&resolution=720*1344&openudid=22ea9722444e15ef&update_version_code=4002&app_name=aweme&version_name=4.0.0&os_api=27&device_brand=xiaomi&ssmix=a&device_platform=android&dpi=320&aid=1128&as=a1358163c4a92ccb346888&cp=1b9bc65c4e493fb7e1Si[m&mas=01a5e34b868b770206185d569c0d8a72791c1c1c6c2ccc46c6c64c";
//        	System.out.println("第二个url："+content);
        	
        	//拼接下一个url
        	String zhenshiurl = "https://api.amemv.com/aweme/v1/aweme/detail/?aweme_id="+videoid+"&retry_type=no_retry&mcc_mnc=46000&iid=56986543517&device_id=57400937868&ac=wifi&channel=update&aid=1128&app_name=aweme&version_code=400&version_name=4.0.0&device_platform=android&ssmix=a&device_type=Redmi+6&device_brand=xiaomi&language=zh&os_api=27&os_version=8.1.0&uuid=99001236660791&openudid=22ea9722444e15ef&manifest_version_code=400&resolution=720*1344&dpi=320&update_version_code=4002&_rticket=1546920855243&ts=1546920854&js_sdk_version=1.6.4&as=a1a592e33629bc43f42077&cp=2a99c35d614d323de1asOa&mas=01d91a7f526377af5559abf6f31b5a2295ecec0c4c2c66cc2cc646";
//        	System.out.println("拼接下一个url:"+zhenshiurl);
        	
        	XiangQingUtils main = new XiangQingUtils();  
            String result = main.test(zhenshiurl);  
//            System.out.println(result);
            DouYinShiPin douyinShiPin = JSON.parseObject(result,new TypeReference<DouYinShiPin>(){});
            System.out.println("douyinShiPin:"+douyinShiPin);
            String miaoshu = douyinShiPin.getAweme_detail().getDesc();
            String zheshirul = douyinShiPin.getAweme_detail().getVideo().getPlay_addr().getUrl_list().get(0).toString();
            String zuozhe = douyinShiPin.getAweme_detail().getAuthor().getNickname();
            String gequming = douyinShiPin.getAweme_detail().getMusic().getTitle();
            FanHui  fanhui = new FanHui();
            fanhui.setZuozhe(filterEmoji(zuozhe));
            fanhui.setMiaoshu(filterEmoji(miaoshu));
            fanhui.setDownloadurl(zheshirul);
            fanhui.setGequming(filterEmoji(gequming));
            return fanhui;
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return null;
    }  
    
    public static void main(String[] args){
    	//拼接下一个url
    	String zhenshiurl = "http://v6-dy-x.ixigua.com/ff6583dd8023d52b17cc8ff46adc5129/5ee09d43/video/tos/cn/tos-cn-ve-15/f096a606947840d6af1d01bf2cd339bc/?a=1128&br=3339&bt=1113&cr=0&cs=0&dr=0&ds=6&er=&l=202106101543350100140470971909D1E6&lr=&mime_type=video_mp4&qs=0&rc=Mzs6Z2tvajQzdTMzNGkzM0ApaWg0ZThkaTw6NztpaDlmN2ctLnIuLWVsb2NfLS1jLS9zc18wMV5hY2EzNDIxYGA0NTM6Yw%3D%3D&vl=&vr=";
//    	System.out.println("拼接下一个url:"+zhenshiurl);
    	
    	XiangQingUtils main = new XiangQingUtils();  
        String result = main.test(zhenshiurl);  
        System.out.println(result);
        DouYinShiPin douyinShiPin = JSON.parseObject(result,new TypeReference<DouYinShiPin>(){});
        System.out.println("douyinShiPin:"+douyinShiPin);
        String miaoshu = douyinShiPin.getAweme_detail().getDesc();
        String zheshirul = douyinShiPin.getAweme_detail().getVideo().getPlay_addr().getUrl_list().get(0).toString();
        String zuozhe = douyinShiPin.getAweme_detail().getAuthor().getNickname();
        String gequming = douyinShiPin.getAweme_detail().getMusic().getTitle();
//        System.out.println(miaoshu);
//        System.out.println(zheshirul);
//        System.out.println(zuozhe);
//        System.out.println(gequming);
    }
    /**
     * 将emoji表情替换成空串
     *  
     * @param source
     * @return 过滤后的字符串
     */ 
    public String filterEmoji(String source) { 
     if (source != null && source.length() > 0) { 
      return source.replaceAll("[\ud800\udc00-\udbff\udfff\ud800-\udfff]", ""); 
     } else { 
      return source; 
     } 
    }
}